package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询角色
     *
     * @param queryPageBean 查询页面豆
     * @return {@link Result}
     */
    @GetMapping("/page")
    public Result findRoleByPage(QueryPageBean queryPageBean){
        try {
            PageResult result = roleService.findRoleByPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @GetMapping("all")
    public Result findAllRole(){
        try {
            List<Role> result = roleService.findAllRole();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 删除角色的id
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("{id}")
    public Result deleteRoleById(@PathVariable("id") Integer id){
        try {
            boolean bool = roleService.deleteRoleById(id);
            if (bool){
                return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
            }
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL_1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 添加角色
     *
     * @param role 角色
     * @return {@link Result}
     */
    @PostMapping
    public Result addRole(@RequestBody Role role,
                          @RequestParam("permissionIds") Integer[] permissionIds,
                          @RequestParam("menuIds") Integer[] menuIds){
        try {
            roleService.addRole(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 更新角色
     *
     * @param role 角色
     * @return {@link Result}
     */
    @PutMapping
    public Result updateRole(@RequestBody Role role,
                             @RequestParam("permissionIds") Integer[] permissionIds,
                             @RequestParam("menuIds") Integer[] menuIds){
        try {
            roleService.updateRole(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 查询单个角色信息
     *
     * @param  id id
     * @return {@link Result}
     */
    @GetMapping("{id}")
    public Result findRoleById(@PathVariable("id") Integer id){
        try {
            Role result = roleService.findRoleById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     *
     *查询角色id关联的菜单ID
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/{id}/menu")
    public Result findMenuIdByRoleId(@PathVariable("id") Integer id){
        try {
            Integer[] menuIds = roleService.findMenuIdByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 通过角色id找到权限id
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/{id}/permission")
    public Result findPermissionIdByRoleId(@PathVariable("id") Integer id){
        try {
            Integer[] permissionIds = roleService.findPermissionIdByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
